package models

import (
	"192.168.1.75/go-pkg/helper"
	"gorm.io/gorm"
)

// AlarmRecord 告警事件
// 这个几乎是实时的，而告警通知是告警满足一定持续时间才进行的通知
type AlarmRecord struct {
	ID           string  `json:"id" gorm:"column:id;primaryKey;type:int;size:64;comment:主键id"`
	RobotID      string  `json:"robot_id" gorm:"column:robot_id;type:string;size:40;not null;index:idx_robot_created_at,priority:1;index:idx_robot_created_date,priority:2;comment:机器人id"`
	MapName      string  `json:"map_name" gorm:"column:map_name;type:string;size:40;comment:地图名称"`
	PosX         float64 `json:"pos_x" gorm:"column:pos_x;type:double;size:64;comment:机器人位置x"`
	PosY         float64 `json:"pos_y" gorm:"column:pos_y;type:double;size:64;comment:机器人位置y"`
	PosDirection float64 `json:"pos_direction" gorm:"column:pos_direction;type:double;size:64;机器人位置direction"`
	AlarmType    int     `json:"alarm_type" gorm:"column:alarm_type;type:int;size:16;index:idx_robot_created_at,priority:3;comment:告警类型"`
	CreatedMonth string  `json:"created_month,omitempty" gorm:"column:created_month;type:string;size:10;index:created_month;comment:告警月份"`
	CreatedDate  string  `json:"created_date,omitempty" gorm:"column:created_date;type:string;size:10;index:idx_robot_created_date,priority:1;comment:告警日期"`
	Duration     int64   `json:"duration" gorm:"column:duration;type:int;size:64;comment:持续时间"`
	ReportedAt   int64   `json:"reported_at" gorm:"column:reported_at;type:uint;autoCreateTime;comment:上报时间"`
	CreatedAt    int64   `json:"created_at" gorm:"autoCreateTime;column:created_at;type:int;size:64;index:created_at;index:idx_robot_created_at,priority:2;comment:创建时间"`
}

// TableName 指定表名
func (AlarmRecord) TableName() string {
	return "alarm_record"
}

// BeforeCreate 自增id
func (a *AlarmRecord) BeforeCreate(tx *gorm.DB) (err error) {
	if a.ID != "" {
		return nil
	}
	// 用户id为空时，自动添加用户id
	uuid, err := helper.UUID{}.String()
	if err != nil {
		return err
	}
	a.ID = uuid
	return nil
}

// 模式自动注册
func init() {
	Models = append(Models, &AlarmRecord{})
}
